package cn.yunhe.service.impl;

import cn.yunhe.dao.EmpDao;
import cn.yunhe.entity.Emp;
import cn.yunhe.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @program: ssm_insurance
 * @description: 员工服务层的实现类(service实现类)
 * @author: 李武
 * @create: 2018-09-14 20:03
 **/
@Service("empService")
public class EmpServiceImpl implements EmpService {
    @Resource
    private EmpDao empDao;

    @Override
    public int addOrUpdatePic(Emp emp) {
        return empDao.addOrUpdatePic(emp);
    }

    @Override
    public int updateEmpPassword(Emp emp) {
        return empDao.updateEmpPassword(emp);
    }

    @Override
    public int addEmp(Map<String, Object> map) {
        int flag = empDao.addEmp(map);
        if (flag > 0) {
            return flag;
        } else {
            return 0;
        }
    }

    @Override
    public int delEmp(Emp emp) {
        int flag = empDao.delEmp(emp);
        if (flag > 0) {
            return flag;
        } else {
            return 0;
        }
    }

    @Override
    public int delsEmp(List<String> euuId) {
        return empDao.delsEmp(euuId);
    }

    @Override
    public Emp getOneEmp(Emp emp) {
        Emp emp1 = empDao.getOneEmp(emp);
        if (emp1 != null) {
            return emp1;
        } else {
            return null;
        }
    }

    @Override
    public int updateEmp(Map<String, Object> map) {
        return empDao.updateEmp(map);
    }

    @Override
    public Page getEmpListById(Map<String, Object> map) {
        Page page = new Page();
        /*获取记录条数,赋给Page工具类中total属性中*/
        page.setTotal(empDao.getEmpListById(map).size());
        /*获取每页大小*/
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        page.setPageSize(pageSize);
        /*获取当前页码*/
        int pageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
        page.setPageNum(pageNum);
        /*计算出总页数*/
        int totalPage = (int) (page.getTotal() / pageSize + (page.getTotal() % pageSize == 0 ? 0 : 1));
        page.setPageNum(totalPage);
        /*启动分页*/
        page = PageHelper.startPage(pageNum, pageSize);
        /*查询员工*/
        List<Emp> empList = empDao.getEmpListById(map);
        return page;
    }
    /*==================================================================*/

    /**
     * 登录操作
     *
     * @param emp
     * @return
     */
    @Override
    public Emp Login(Emp emp) {
        return empDao.login(emp);
    }

    /**
     * 更改登录状态
     *
     * @param emp
     * @return
     */
    @Override
    public boolean updateLoginState(Emp emp) {
        return empDao.updateLoginState(emp);
    }

    /**
     * 更改密码
     *
     * @param emp
     * @return
     */
    @Override
    public boolean updatePassword(Emp emp) {
        return empDao.updatePassword(emp);
    }

    /**
     * 更改账户状态
     *
     * @param emp
     * @return
     */
    @Override
    public boolean updateAccountState(Emp emp) {
        return empDao.updateAccountState(emp);
    }

    public EmpDao getEmpDao() {
        return empDao;
    }

    public void setEmpDao(EmpDao empDao) {
        this.empDao = empDao;
    }
}
